import http from "@/utils/http.ts";
import {Dict, Page, PageResult, R} from "@/api/common.ts";

export interface QualityModel {
    type: number;
    modelId: number;
    id: string;
}

export interface TrainModel {
    name: string;
    featureStep: number;
    labelStep: number;
    testRate: number;
    epoch: number;
    batchSize: number;
    featureStart: number;
    featureStop: number;
    labelStart: number;
    labelStop: number;
    timeWindow: boolean;
    skip: number;
    standard: boolean;
    lstm: TrainModelLstm[];
}

export interface LstmListDataRecordsLstmList {
    id: number;
    learningRate: number;
    hiddenCell: number;
    l2: number;
    updater: number;
    index: number;
    modelId: number;
}

export interface LstmListDataRecords {
    id: number;
    name: string;
    url: string;
    featureStep: number;
    labelStep: number;
    testRate: number;
    epoch: number;
    batchSize: number;
    featureStart: number;
    featureStop: number;
    labelStart: number;
    labelStop: number;
    skip: number;
    standard: boolean;
    timeWindow: boolean;
    status: number;
    userId: number;
    lstmList: LstmListDataRecordsLstmList[];
}

export interface PredictData {
    modelId: number;
    input: number[][];
}

export interface PredictMultiData extends PredictData {
    step: number;
}

export interface TrainModelLstm {
    learningRate: number;
    hiddenCell: number;
    l2: number;
    updater: number;
}

interface DictList {
    status: Dict[],
    update: Dict[]
}

/**
 * 获取关联模型信息
 * @param data
 */
export function pageModelInfo(data: QualityModel & Page) {
    return http<R<PageResult<LstmListDataRecords>>>({
        url: "qualityModel/page",
        method: "POST",
        data
    })
}

/**
 * 关联空气质量模型
 * @param data
 */
export function associateQuality(data: QualityModel) {
    return http<R<any>>({
        url: "qualityModel/associate",
        method: "post",
        data
    })
}

export function removeAssociate(id:number) {
    return http<R<any>>({
        url:"qualityModel/"+id,
        method:"delete"
    })
}

/**
 * 分页查询自己训练的模型
 * @param data
 */
export function pageModels(data: Page & { id: number }) {
    return http<R<PageResult<LstmListDataRecords>>>({
        url: "model/page",
        method: "POST",
        data
    })
}

export function getUpdater() {
    return http<R<DictList>>({
        url: "model/dict",
        method: "get"
    })
}

/**
 * 训练模型
 * @param data
 */
export function trainLstm(data: FormData) {
    return http<R<any>>({
        url: "model",
        method: "post",
        data: data
    })
}

/**
 * 预测模型
 * @param data
 */
export function predict(data: PredictData) {
    return http<R<any>>({
        url: "model/predict",
        method: "post",
        data
    })
}

/**
 * 多步预测
 * @param data
 */
export function predictStep(data: PredictMultiData) {
    return http<R<any>>({
        url: "model/predict/step",
        method: "post",
        data
    })
}

export function getModelList() {
    return http<R<LstmListDataRecords[]>>({
        url: "model",
        method: "get"
    })
}

export function deleteModel(id: string) {
    return http<R<any>>({
        url: "model/" + id,
        method: "delete"
    })
}